package com.zhijiang.util;


import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


@Slf4j
public class MapUtil {
    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 通过经纬度获取距离(单位：米)
     *
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return 距离
     */

    public static double getDistance(double lat1, double lng1, double lat2,
                                     double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s * 1000;
        return s;
    }


    public static boolean isInCircle(double lng1, double lat1, double lng2, double lat2, String radius) {
        double distance = getDistance(lat1, lng1, lat2, lng2);
        double r = Double.parseDouble(radius);
        if (distance > r) {
            return false;
        } else {
            return true;
        }
    }


//    /**
//     * 通过传递两个地方的地址,判断以自己为圆心, 别人地址是否存在于圆半径内
//     * @param myAddress 自己地址
//     * @param otherAddress 别人地址
//     * @param radius 圆心半径 ,单位:米
//     * @return
//     */
//    public static boolean isInCircle(String myAddress,String otherAddress,double radius){
//        Point myPoint = calAddressToPoint (myAddress);
//        Point otherPoint = calAddressToPoint (otherAddress);
//        double distance = calJuliByTwoPoint (myPoint, otherPoint);
//        if (distance > radius) {
//            return false;
//        } else {
//            return true;
//        }
//    }

//    public static void main(String[] args) {
//        double distance = getDistance(30.491252, 114.407765,
//                30.48986, 114.406729);
//        if (distance>100){
//            System.out.println("不在规定范围");
//        }else {
//            System.out.println("距离" + distance / 1000 + "公里");
//        }
//
//    }

    //将获取到的位置信息转换为经纬度
    public static Map<String, Object> getURLContent(String address) {
        //这里需要使用你的key值
        String urlStr = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + address + "&key=HCVBZ-WP6KD-RSL4P-PK3DW-2JR4Z-NLFC5";
        //请求的url
        URL url = null;
        //请求的输入流
        BufferedReader in = null;
        //输入流的缓冲
        StringBuffer sb = new StringBuffer();
        try {
            url = new URL(urlStr);
            in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            String str = null;
            //一行一行进行读入
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
        } catch (Exception ex) {

        } finally {
            try {
                if (in != null) {
                    in.close(); //关闭流
                }
            } catch (IOException ex) {

            }
        }
        String result = sb.toString();

        String r = JSONObject.fromObject(result).getString("result");
        log.info("异常信息{}", r);
        String location = JSONObject.fromObject(r).getString("location");
        String lng = JSONObject.fromObject(location).getString("lng");
        String lat = JSONObject.fromObject(location).getString("lat");
        Map<String, Object> map = new HashMap<>();
        map.put("lng", lng);
        map.put("lat", lat);
        return map;
    }
//    public static void main(String[] args) {
//        Map<String, Object> map = getURLContent("湖北省武汉市洪山区保利时代南区九栋一单元");
//        System.out.println("lng=" + map.get("lng") + ",lat=" + map.get("lat"));
//
//    }
//    public static void main(String[] args) {
//        Map<String, Object> zjHospitalmap = MapUtil.getURLContent("枝江市人民医院");
////        System.out.println(zjHospitalmap);
//        log.info("经纬度为{}",zjHospitalmap);
//        log.info("维度为{}",zjHospitalmap.get("lat"));
//        log.info("经度为{}",zjHospitalmap.get("lng"));
//
////        System.out.println(zjHospitalmap.get("lng"));
////        System.out.println(zjHospitalmap.get("lat"));
//        Map<String, Object> zjDongMingMap = MapUtil.getURLContent("枝江市妇幼保健院");
//        log.info("经纬度为{}",zjDongMingMap);
//        log.info("维度为{}",zjDongMingMap.get("lat"));
//        log.info("经度为{}",zjDongMingMap.get("lng"));
//
//        double distance = MapUtil.getDistance(Double.parseDouble(zjHospitalmap.get("lat").toString()), Double.parseDouble(zjHospitalmap.get("lng").toString()),
//                Double.parseDouble(zjDongMingMap.get("lat").toString()), Double.parseDouble(zjDongMingMap.get("lng").toString()));
//        if (distance>1000){
//            throw new RuntimeException("签到失败,未在指定范围内签到,请到指定的范围内签到");
//        }
//        log.info("执行成功");
//    }
    /**
     * 通过腾讯地图将经纬度转成详细地址
     *
     * @param longitude 纬度
     * @param latitude 经度
     * @return
     */


}
